//
//  HMDiscoverSearchView.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/21.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMDiscoverSearchView: UIView ,UITextFieldDelegate{

    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
    @IBOutlet weak var cancleButton: UIButton!
    @IBOutlet weak var textFiled: UITextField!

    @IBAction func CancelButtonClick(sender: UIButton) {
        textFiled.resignFirstResponder()
        textFieldRightCons.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textFiled.layoutIfNeeded()
        }
    }
    //与代码创建控件有关
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    //xib 里面将线与连好连好之后会调用这个方法
    override func awakeFromNib() {
        //textFiled.layer.borderWidth
        //设置leftView
        let  leftView = UIImageView(image: UIImage(named: "visitordiscover_image_profile"))
        //设置内容显示模式
        //leftView.contentMode = .Center
        
        textFiled.leftView = leftView
        //设置显示模式
        textFiled.leftViewMode = UITextFieldViewMode.Always
        textFiled.leftView?.frame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
    }
    //文件转view的阶段
    //可以通过代码的往当前控件添加子控件
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    class func searchView() ->HMDiscoverSearchView {
        return NSBundle.mainBundle().loadNibNamed("HMDiscoverSearchView", owner: nil, options: nil).last! as! HMDiscoverSearchView
    }
    
    // MARK: - UITextFieldDelegate
    
    func textFieldDidBeginEditing(textField: UITextField) {
        textFieldRightCons.constant = cancleButton.frame.width
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textFiled.layoutIfNeeded()
        }
    }

}
